package com.example.shixun1.renttest;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shixun1.R;
import com.example.shixun1.renttest.main.MainActivity;
import com.example.shixun1.renttest.util.OkHttpUtil;
import com.google.Gson;

import java.io.IOException;

import okhttp3.FormBody;

public class LoginActivity extends AppCompatActivity implements OnTouchListener {

	private static final String TAG = "0";
	private Button back_btn,login_btn;
	private TextView register_btn;
	private EditText userName_et,passWord_et;
	private Handler hand = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.arg2 == 0){
				Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
			}
			if(msg.arg2 == 1){
				Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(intent);
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login2);
		initView();
	}
	private void initView(){
//		back_btn = (Button) findViewById(R.id.Login_back_btn);
		login_btn = (Button) findViewById(R.id.Login_login_btn);
		register_btn =  findViewById(R.id.Login_register_btn);
		userName_et = (EditText) findViewById(R.id.Login_username);
		passWord_et = (EditText) findViewById(R.id.Login_password);
//		back_btn.setOnTouchListener(this);
		login_btn.setOnTouchListener(this);
		register_btn.setOnTouchListener(this);
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch(v.getId()){
//		case R.id.Login_back_btn:
//			finish();
//			break;
		case R.id.Login_login_btn:
			
			if(event.getAction() == MotionEvent.ACTION_DOWN){
//				login_btn.setBackgroundResource(R.drawable.ump_tab_center_off);

				new Thread(new Runnable() {
					@Override
					public void run() {


						login(userName_et.getText().toString(), passWord_et.getText().toString(), true);


					}
				}).start();


			}if(event.getAction() == MotionEvent.ACTION_UP){
//				login_btn.setBackgroundResource(R.drawable.ump_tab_center_on);
			
			}
			
			
			
			
			
			break;
		case R.id.Login_register_btn:
			if(event.getAction() == MotionEvent.ACTION_DOWN){
//				register_btn.setBackgroundResource(R.drawable.ump_tab_center_on);
			}if(event.getAction() == MotionEvent.ACTION_UP){
//				register_btn.setBackgroundResource(R.drawable.ump_tab_center_off);
				Intent intentRegister = new Intent(LoginActivity.this,RegisterActivity.class);
				startActivity(intentRegister);
			}
			break;
		}
		return true;
	}

	/**
	 * 网络请求登录
	 * @param name
	 * @param password
	 * @param isRemember
	 */
	private void login(String name, String password, final boolean isRemember) {
		String url = "http://"+getResources().getString(R.string.ip)+"/foodserver/user/login2";
		FormBody body = new FormBody.Builder()
				.add("name",name)
				.add("password",password)
				.add("telephone",name)
				.build();
		try {
			String result = OkHttpUtil.postForParams(url, body);
			Gson gson = new Gson();
			final StudentBean studentBean = gson.fromJson(result, StudentBean.class);
			final boolean success = studentBean.isSuccess();

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if(success){
						MyApplication.saveUser(studentBean.getData(),LoginActivity.this,isRemember);
						startActivity(new Intent(LoginActivity.this, MainActivity.class));
						finish();
					}else{
						Toast.makeText(LoginActivity.this, "登录失败，用户名或密码错误", Toast.LENGTH_SHORT).show();
					}
				}
			});


		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 